package com.hhsjtest.scaffold.abs;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.hhsjtest.scaffold.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;

public abstract class ScaffoldView<T extends View> extends RelativeLayout {

    //1，滚动到顶部
    public static final int SCROLL_TO_TOP = 1;

    private boolean isDebug = false;
    private int mFloatBarActionType = SCROLL_TO_TOP;

    //顶部导航
    protected LinearLayout topNavigationView;
    //主干滚动视图
    protected T contentScrollView;
    //悬浮按钮
    protected FrameLayout floatBarView;

    public ScaffoldView(Context context) {
        this(context, null);
    }

    public ScaffoldView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScaffoldView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        try {
            Class<T> tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            contentScrollView = tClass.getDeclaredConstructor(Context.class).newInstance(getContext());

            contentScrollView.setId(R.id.scaffold_scrollview);
            if (isDebug) {
                contentScrollView.setBackgroundColor(0xFF0000FF);
            }

            LayoutParams params1 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            params1.addRule(ALIGN_PARENT_START);
            params1.addRule(ALIGN_PARENT_END);
            params1.addRule(ALIGN_PARENT_BOTTOM);
            addView(contentScrollView, params1);

        } catch (IllegalAccessException
                | InstantiationException
                | InvocationTargetException
                | NoSuchMethodException
                | NullPointerException e) {
            e.printStackTrace();
        }

        topNavigationView = new LinearLayout(getContext());
        topNavigationView.setId(R.id.scaffold_navigationview);
        topNavigationView.setOrientation(LinearLayout.VERTICAL);

        if (isDebug) {
            topNavigationView.setBackgroundColor(0xFF00FF00);
        }

        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.alignWithParent = true;
        if (contentScrollView != null) {
            params.addRule(ABOVE, contentScrollView.getId());
        }
        params.addRule(ALIGN_PARENT_START);
        params.addRule(ALIGN_PARENT_END);
        params.addRule(ALIGN_PARENT_TOP);
        addView(topNavigationView, params);

        floatBarView = new FrameLayout(getContext());
        floatBarView.setId(R.id.scaffold_floatview);

        if (isDebug) {
            floatBarView.setBackgroundColor(0xFFFF0000);
        }

        LayoutParams params2 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params2.addRule(ALIGN_PARENT_END);
        params2.addRule(ALIGN_PARENT_BOTTOM);
        params2.rightMargin = 200;
        params2.bottomMargin = 200;
        addView(floatBarView, params2);

        floatBarView.setOnClickListener(view -> {
            if (mFloatBarActionType == SCROLL_TO_TOP) {
                scrollToTop();
            } else if (mFloatBarActionType == 2) {
                contentScrollView.scrollTo(0, 0);
            }
        });
    }

    protected abstract void scrollToTop();

    public void setDebug(boolean debug) {
        this.isDebug = debug;
        if (debug) {
            if (contentScrollView != null) {
                contentScrollView.setBackgroundColor(0xFF0000FF);
            }
            topNavigationView.setBackgroundColor(0xFF00FF00);
            floatBarView.setBackgroundColor(0xFFFF0000);
        }
    }

    //悬浮按钮
    public void setFloatBarViewMargin(int marginRight, int marginBottom) {
        LayoutParams lp = (LayoutParams) floatBarView.getLayoutParams();
        lp.rightMargin = marginRight;
        lp.bottomMargin = marginBottom;
    }

    //2，弹出弹窗
    public void setFloatBarAction(int type) {
        mFloatBarActionType = type;
    }

    public void addFloatBarView(@NonNull View view) {
        if (floatBarView.getChildCount() > 0 && view == floatBarView.getChildAt(0)) {
            return;
        }
        floatBarView.removeAllViews();
        floatBarView.addView(view);
    }

    /**
     * 添加头部布局
     *
     * @param view
     */
    public void addTopNavigationView(@NonNull View view) {
        if (topNavigationView.getChildCount() > 0 && view == topNavigationView.getChildAt(0)) {
            return;
        }
        topNavigationView.removeAllViews();
        topNavigationView.addView(view);
    }
}
